home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Trading on the Edge
/
Trading On The Edge - CD-ROM Toolkit (Wayzata Technology)(2031)(1994).bin
/
pc
/
pc_files
/
venddemo
/
backprop
/
nnrexpl.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-06-08
|
6KB
|
180 lines
/* nnrexpl - rank the % importance of input PEs using Explain output. */
/* assumptions: - no input data is logged in the *.nnr file */
/* - there are no headers in the *.nnr file */
/* - the *.nnr file is NOT appended to */
/* note: accurate results require that the Explain function be run on */
/* a file containing all training and testing data sets. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAXLINELEN 1024*5 /* longest line read from *.nnr file */
/* #define NUMINS 4 */ /* number of input PEs; avoids prompting */
main(argc, argv)
int argc;
char **argv;
{
FILE *f1, *f2;
char inputLine[MAXLINELEN];
char filename[100]; /* input filename */
int fieldCount; /* number of fields per data case in nnr file */
float *column; /* pointer to dynamic memory (column totals) */
float grandTotal; /* total of column totals */
float subTotal; /* for nets with multiple output PEs */
long tmp;
int numIns; /* inputs for this net (user specified) */
int numOuts; /* outputs for this net (fieldcount/numIns) */
int i, j;
/* open i/o files */
if (argc == 2)
strcpy (filename, argv[1]);
else
{
printf ("Enter the *.nnr results filename... ");
gets (filename);
}
i = strlen (filename);
for (j = 0; filename[j] != '.' && j < i; j++); /* locate period */
if (j == i)
strcat (filename, ".nnr"); /* default the extension */
if ( (f1 = fopen (filename, "r")) == (FILE *)0)
{
printf ("Cannot open input file '%s', aborting.\n", filename);
exit (1);
}
if ( (f2 = fopen ("nnrexpl.out", "w")) == (FILE *)0)
{
printf ("Cannot open output file 'nnrexpl.out', aborting.\n");
exit (1);
}
/* prompt for the number of input PEs in this data */
#ifdef NUMINS
numIns = NUMINS;
#else
numIns = 0;
printf ("Enter the number of input PEs in this network... ");
while (1)
{
gets (inputLine);
numIns = atoi (inputLine);
if (numIns != 0)
break;
printf ("Invalid entry; please re-enter the number of input PEs... ");
}
#endif
/* find the first input record */
fgets (inputLine, MAXLINELEN, f1);
if (inputLine[0] == '\n')
fgets (inputLine, MAXLINELEN, f1);
/* count the number of fields in the first record */
fieldCount = 0;
for (tmp = 0; inputLine[tmp] != '\0'; tmp++)
if (inputLine[tmp] > 0x20 && inputLine[tmp+1] <= 0x20)
fieldCount++;
numOuts = fieldCount / numIns;
if (numIns * numOuts != fieldCount)
{
printf ("Error reading data file, or incorrect number of input PEs specified. \n");
printf ("%d inputs is not a multiple of %d fields detected in data; aborting.\n", numIns, fieldCount);
exit (1);
}
/* allocate memory for the accumulation and init the array */
column = NULL;
column = (float *)calloc (fieldCount, sizeof(float)); /* array of column outputs from 1 data case */
if (column == NULL)
{
printf ("Insufficient memory to check results; aborting.");
fclose (f1);
exit (1);
}
for (i=0; i < fieldCount; i++)
column[i] = 0.0;
/* accumulate the values */
while (1)
{
tmp = 0;
for (i=0; i != fieldCount; i++)
{
while (inputLine[tmp] <= 0x20)
tmp++;
grandTotal = 0.0;
sscanf(&inputLine[tmp], "%f", &grandTotal);
column[i] += fabs(grandTotal);
while (inputLine[tmp] > 0x20)
tmp++;
}
/* read the next data case */
if (fgets (inputLine, MAXLINELEN, f1) == 0)
break; /* eof */
if (inputLine[0] == '\n')
fgets (inputLine, MAXLINELEN, f1);
} /* end while */
fclose (f1);
/* calculate the grand total */
grandTotal = 0.0;
for (i=0; i != fieldCount; i++)
grandTotal += column[i]; /* total of all column totals */
/* display and log results */
/* pass 1.. rank by number of inputs */
subTotal = 0.0;
printf ("\nExplain Summary for %s, %d input and %d output PEs.\n", filename, numIns, numOuts);
printf ("relative overall importance of input variables...\n");
fprintf (f2, "relative overall importance of input variables...\n");
for (i=0; i < fieldCount; i++)
{
if ( (i+1) % numOuts == 0)
{
subTotal += column[i];
printf (" input%4d %6.0f %6.1f%%\n",
(int)(i/numOuts+1), subTotal, (subTotal/grandTotal)*100.0);
fprintf (f2, " input%4d %6.0f %6.1f%%\n",
(int)(i/numOuts+1), subTotal, (subTotal/grandTotal)*100.0);
subTotal = 0.0;
}
else
{
subTotal += column[i];
}
}
/* pass 2.. if multiple outputs, rank by number of outputs */
if (numOuts > 1)
{
printf ("relative importance of inputs per output PE...\n");
fprintf (f2, "relative importance of inputs per output PE...\n");
for (i=0; i < numOuts; i++)
{
printf ("output%4d\n", i+1);
fprintf (f2, "output%4d\n", i+1);
grandTotal = 0.0;
for (j=0; j < numIns; j++)
grandTotal += column[i+j*numOuts];
for (j=0; j < numIns; j++)
{
printf (" input%4d %6.0f %6.1f%%\n",
j+1, column[i+j*numOuts], (column[i+j*numOuts]/grandTotal)*100.0);
fprintf (f2, " input%4d %6.0f %6.1f%%\n",
j+1, column[i+j*numOuts], (column[i+j*numOuts]/grandTotal)*100.0);
}
}
}
fclose (f2);
printf ("Output file is nnrexpl.out.");
free (column);
exit (0);
}